package org.finir.fakturisanje.rich.web.page;

import java.io.Serializable;
import java.util.List;

import org.finir.fakturisanje.business.service.KontrolerAL;
import org.finir.fakturisanje.persistence.Komitent;
import org.finir.fakturisanje.persistence.Preduzece;
import org.finir.fakturisanje.persistence.beans.KomitentBean;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.log.Log;

@Name("komitentPage")
@Scope(ScopeType.PAGE)
public class KomitentPage implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1843639396624979834L;

	@Logger
	Log log;

	@In(required = true)
	Preduzece preduzeceLoggedIn;

	@In(create = true)
	@Out(required = false)
	@DataModelSelection("listKomitent")
	Komitent komitent;

	@In("#{kontrolerAL}")
	KontrolerAL kontrolerAL;

	@SuppressWarnings("unused")
	@DataModel("listKomitent")
	private List<Komitent> listKomitent;

	public void save() {

		log.info("save");
		komitent.setPreduzece(preduzeceLoggedIn);
		kontrolerAL.sacuvajEntitet(komitent);
		createNew();
		refresh();

	}

	public void select() {
		log.info("select");

	}

	public void refresh() {
		log.info("refresh ");
		listKomitent = kontrolerAL.vratiListuKomitenata(preduzeceLoggedIn);
	}

	public void createNew() {
		log.info("create new");
		komitent = new KomitentBean();

	}

	@Factory("listKomitent")
	public void loadAll() {
		log.info("load ");
		listKomitent = kontrolerAL.vratiListuKomitenata(preduzeceLoggedIn);

	}

	public void delete() {
		log.info("delete " + komitent.getId());
		kontrolerAL.obrisiEntitet(komitent);
		createNew();
		refresh();

	}

	public void update() {
		log.info("update " + komitent.getId());
		kontrolerAL.izmeniEntet(komitent);
		createNew();
		refresh();
	}

	public String cancel() {
		return "/home.xhtml";
	}
}
